package com.pkk.geer.rose.service.professional;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.pkk.geer.rose.common.constant.GeerRoseConstant;
import com.pkk.geer.rose.common.enums.RoseSpeenReqTypeEnum;
import com.pkk.geer.rose.common.utils.RoseSpeedAppUtil;
import com.pkk.geer.rose.common.vo.RoseSpeendAppHttpParamVo;
import com.pkk.geer.rose.entity.RoseSpeedAppEntity;

import com.pkk.geer.rose.common.req.ConstellationLuckReqData;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @description: 星座小应用的业务类
 * @author: peikunkun
 * @create: 2019-11-07 18:23
 **/
@Slf4j
@Service
public class ConstellationAppService {

  @Resource
  private RoseSpeedAppService roseSpeedAppService;

  /**
   * @Description: 处理星座运势的数据
   * @Param: [reqData]
   * @return: java.util.ArrayList<java.lang.String>
   * @Author: peikunkun
   * @Date: 2019/11/9 0009 下午 1:58
   */
  public JSONObject handleConstellationLucks(ConstellationLuckReqData reqData) {
    AtomicReference<JSONObject> jsonObject = new AtomicReference<>(new JSONObject());
    final List<RoseSpeedAppEntity> roseSpeedAppByAppFlag = roseSpeedAppService
        .getRoseSpeedAppByAppFlag(GeerRoseConstant.APP_PLAG_GGER);
    roseSpeedAppByAppFlag.stream().forEach(r -> {
      final RoseSpeenReqTypeEnum handleType = RoseSpeenReqTypeEnum.handleType(r.getHandleType());
      final RoseSpeendAppHttpParamVo roseSpeendAppHttpParamVo = RoseSpeedAppUtil
          .parseHttReq(handleType, r.getReqParam(), r.getReqValue(), r.getAppKey());
      final String finalReqPath = r.getAppUrl().concat(roseSpeendAppHttpParamVo.getReqPath());
      final String responseStr = HttpUtil.get(finalReqPath, GeerRoseConstant.HTTP_TIME_OUT_FIVE_M);
      final JSONObject responseJson = JSONObject.parseObject(responseStr);
      if ("0".equals(responseJson.getString("status"))) {
        jsonObject.set(responseJson.getJSONObject("result").getJSONObject("today"));
      }
    });
    return jsonObject.get();
  }
}
